home *** CD-ROM | disk | FTP | other *** search
/ Motor Sport Digital Archive Collection 1960s / Motor Sport Digital Archive Collection 1960s.iso / main.swf / scripts / mx / styles / StyleManagerImpl.as < prev    next >
Encoding:
Text File  |  2008-05-21  |  15.2 KB  |  501 lines

  1. package mx.styles
  2. {
  3.    import flash.events.IEventDispatcher;
  4.    import flash.events.TimerEvent;
  5.    import flash.system.ApplicationDomain;
  6.    import flash.system.SecurityDomain;
  7.    import flash.utils.Timer;
  8.    import mx.core.mx_internal;
  9.    import mx.events.ModuleEvent;
  10.    import mx.events.StyleEvent;
  11.    import mx.managers.SystemManager;
  12.    import mx.managers.SystemManagerGlobals;
  13.    import mx.modules.IModuleInfo;
  14.    import mx.modules.ModuleManager;
  15.    import mx.resources.ResourceBundle;
  16.    import mx.utils.StringUtil;
  17.    
  18.    use namespace mx_internal;
  19.    
  20.    public class StyleManagerImpl implements IStyleManager
  21.    {
  22.       private static var instance:IStyleManager;
  23.       
  24.       mx_internal static const VERSION:String = "2.0.1.0";
  25.       
  26.       private static var inheritingTextFormatStyles:Object = {
  27.          "align":true,
  28.          "bold":true,
  29.          "color":true,
  30.          "font":true,
  31.          "indent":true,
  32.          "italic":true,
  33.          "size":true
  34.       };
  35.       
  36.       private static var sizeInvalidatingStyles:Object = {
  37.          "borderStyle":true,
  38.          "borderThickness":true,
  39.          "fontAntiAliasType":true,
  40.          "fontFamily":true,
  41.          "fontGridFitType":true,
  42.          "fontSharpness":true,
  43.          "fontSize":true,
  44.          "fontStyle":true,
  45.          "fontThickness":true,
  46.          "fontWeight":true,
  47.          "headerHeight":true,
  48.          "horizontalAlign":true,
  49.          "horizontalGap":true,
  50.          "kerning":true,
  51.          "leading":true,
  52.          "letterSpacing":true,
  53.          "paddingBottom":true,
  54.          "paddingLeft":true,
  55.          "paddingRight":true,
  56.          "paddingTop":true,
  57.          "strokeWidth":true,
  58.          "tabHeight":true,
  59.          "tabWidth":true,
  60.          "verticalAlign":true,
  61.          "verticalGap":true
  62.       };
  63.       
  64.       private static var parentSizeInvalidatingStyles:Object = {
  65.          "bottom":true,
  66.          "horizontalCenter":true,
  67.          "left":true,
  68.          "right":true,
  69.          "top":true,
  70.          "verticalCenter":true
  71.       };
  72.       
  73.       private static var parentDisplayListInvalidatingStyles:Object = {
  74.          "bottom":true,
  75.          "horizontalCenter":true,
  76.          "left":true,
  77.          "right":true,
  78.          "top":true,
  79.          "verticalCenter":true
  80.       };
  81.       
  82.       private static var colorNames:Object = {
  83.          "transparent":"transparent",
  84.          "black":0,
  85.          "blue":255,
  86.          "green":32768,
  87.          "gray":8421504,
  88.          "silver":12632256,
  89.          "lime":65280,
  90.          "olive":8421376,
  91.          "white":16777215,
  92.          "yellow":16776960,
  93.          "maroon":8388608,
  94.          "navy":128,
  95.          "red":16711680,
  96.          "purple":8388736,
  97.          "teal":32896,
  98.          "fuchsia":16711935,
  99.          "aqua":65535,
  100.          "magenta":16711935,
  101.          "cyan":65535,
  102.          "halogreen":8453965,
  103.          "haloblue":40447,
  104.          "haloorange":16758272,
  105.          "halosilver":11455193
  106.       };
  107.       
  108.       private var resourceUnableToLoad:String = null;
  109.       
  110.       private var _stylesRoot:Object;
  111.       
  112.       private var styleModules:Object;
  113.       
  114.       private var packageResources:ResourceBundle;
  115.       
  116.       private var _inheritingStyles:Object;
  117.       
  118.       private var _typeSelectorCache:Object;
  119.       
  120.       private var selectors:Object;
  121.       
  122.       public function StyleManagerImpl()
  123.       {
  124.          packageResources = ResourceBundle.getResourceBundle("styles",ApplicationDomain.currentDomain);
  125.          resourceUnableToLoad = null;
  126.          selectors = {};
  127.          styleModules = {};
  128.          _inheritingStyles = {};
  129.          _typeSelectorCache = {};
  130.          super();
  131.       }
  132.       
  133.       public static function getInstance() : IStyleManager
  134.       {
  135.          if(!instance)
  136.          {
  137.             instance = new StyleManagerImpl();
  138.          }
  139.          return instance;
  140.       }
  141.       
  142.       public function setStyleDeclaration(param1:String, param2:CSSStyleDeclaration, param3:Boolean) : void
  143.       {
  144.          ++param2.mx_internal::selectorRefCount;
  145.          selectors[param1] = param2;
  146.          typeSelectorCache = new Object();
  147.          if(param3)
  148.          {
  149.             styleDeclarationsChanged();
  150.          }
  151.       }
  152.       
  153.       public function registerSizeInvalidatingStyle(param1:String) : void
  154.       {
  155.          sizeInvalidatingStyles[param1] = true;
  156.       }
  157.       
  158.       public function isColorName(param1:String) : Boolean
  159.       {
  160.          return colorNames[param1.toLowerCase()] !== undefined;
  161.       }
  162.       
  163.       public function registerParentDisplayListInvalidatingStyle(param1:String) : void
  164.       {
  165.          parentDisplayListInvalidatingStyles[param1] = true;
  166.       }
  167.       
  168.       public function set inheritingStyles(param1:Object) : void
  169.       {
  170.          _inheritingStyles = param1;
  171.       }
  172.       
  173.       public function getColorNames(param1:Array) : void
  174.       {
  175.          var _loc2_:int = 0;
  176.          var _loc3_:int = 0;
  177.          var _loc4_:uint = 0;
  178.          if(!param1)
  179.          {
  180.             return;
  181.          }
  182.          _loc2_ = int(param1.length);
  183.          _loc3_ = 0;
  184.          while(_loc3_ < _loc2_)
  185.          {
  186.             if(param1[_loc3_] != null && isNaN(param1[_loc3_]))
  187.             {
  188.                _loc4_ = getColorName(param1[_loc3_]);
  189.                if(_loc4_ != StyleManager.NOT_A_COLOR)
  190.                {
  191.                   param1[_loc3_] = _loc4_;
  192.                }
  193.             }
  194.             _loc3_++;
  195.          }
  196.       }
  197.       
  198.       public function isInheritingTextFormatStyle(param1:String) : Boolean
  199.       {
  200.          return inheritingTextFormatStyles[param1] == true;
  201.       }
  202.       
  203.       public function set typeSelectorCache(param1:Object) : void
  204.       {
  205.          _typeSelectorCache = param1;
  206.       }
  207.       
  208.       public function registerParentSizeInvalidatingStyle(param1:String) : void
  209.       {
  210.          parentSizeInvalidatingStyles[param1] = true;
  211.       }
  212.       
  213.       public function registerColorName(param1:String, param2:uint) : void
  214.       {
  215.          colorNames[param1.toLowerCase()] = param2;
  216.       }
  217.       
  218.       public function isParentSizeInvalidatingStyle(param1:String) : Boolean
  219.       {
  220.          return parentSizeInvalidatingStyles[param1] == true;
  221.       }
  222.       
  223.       public function get inheritingStyles() : Object
  224.       {
  225.          return _inheritingStyles;
  226.       }
  227.       
  228.       public function isParentDisplayListInvalidatingStyle(param1:String) : Boolean
  229.       {
  230.          return parentDisplayListInvalidatingStyles[param1] == true;
  231.       }
  232.       
  233.       public function isSizeInvalidatingStyle(param1:String) : Boolean
  234.       {
  235.          return sizeInvalidatingStyles[param1] == true;
  236.       }
  237.       
  238.       public function set stylesRoot(param1:Object) : void
  239.       {
  240.          _stylesRoot = param1;
  241.       }
  242.       
  243.       public function styleDeclarationsChanged() : void
  244.       {
  245.          var _loc1_:Array = null;
  246.          var _loc2_:int = 0;
  247.          var _loc3_:int = 0;
  248.          var _loc4_:SystemManager = null;
  249.          _loc1_ = SystemManagerGlobals.topLevelSystemManagers;
  250.          _loc2_ = int(_loc1_.length);
  251.          _loc3_ = 0;
  252.          while(_loc3_ < _loc2_)
  253.          {
  254.             _loc4_ = SystemManager(_loc1_[_loc3_]);
  255.             _loc4_.mx_internal::regenerateStyleCache(true);
  256.             _loc4_.mx_internal::notifyStyleChangeInChildren(null,true);
  257.             _loc3_++;
  258.          }
  259.       }
  260.       
  261.       public function isValidStyleValue(param1:*) : Boolean
  262.       {
  263.          return param1 !== undefined;
  264.       }
  265.       
  266.       public function loadStyleDeclarations(param1:String, param2:Boolean = true, param3:Boolean = false) : IEventDispatcher
  267.       {
  268.          var module:IModuleInfo = null;
  269.          var readyHandler:Function = null;
  270.          var styleEventDispatcher:StyleEventDispatcher = null;
  271.          var errorHandler:Function = null;
  272.          var timer:Timer = null;
  273.          var timerHandler:Function = null;
  274.          var url:String = param1;
  275.          var update:Boolean = param2;
  276.          var trustContent:Boolean = param3;
  277.          module = ModuleManager.getModule(url);
  278.          readyHandler = function(param1:ModuleEvent):void
  279.          {
  280.             var _loc2_:IStyleModule = null;
  281.             _loc2_ = IStyleModule(param1.module.factory.create());
  282.             styleModules[param1.module.url].styleModule = _loc2_;
  283.             if(update)
  284.             {
  285.                styleDeclarationsChanged();
  286.             }
  287.          };
  288.          module.addEventListener(ModuleEvent.READY,readyHandler,false,0,true);
  289.          styleEventDispatcher = new StyleEventDispatcher(module);
  290.          errorHandler = function(param1:ModuleEvent):void
  291.          {
  292.             var _loc2_:String = null;
  293.             var _loc3_:StyleEvent = null;
  294.             if(resourceUnableToLoad == null)
  295.             {
  296.                loadResources();
  297.             }
  298.             _loc2_ = StringUtil.substitute(resourceUnableToLoad,param1.errorText,url);
  299.             if(styleEventDispatcher.willTrigger(StyleEvent.ERROR))
  300.             {
  301.                _loc3_ = new StyleEvent(StyleEvent.ERROR,param1.bubbles,param1.cancelable);
  302.                _loc3_.bytesLoaded = 0;
  303.                _loc3_.bytesTotal = 0;
  304.                _loc3_.errorText = _loc2_;
  305.                styleEventDispatcher.dispatchEvent(_loc3_);
  306.                return;
  307.             }
  308.             throw new Error(_loc2_);
  309.          };
  310.          module.addEventListener(ModuleEvent.ERROR,errorHandler,false,0,true);
  311.          styleModules[url] = new StyleModuleInfo(module,readyHandler,errorHandler);
  312.          timer = new Timer(0);
  313.          timerHandler = function(param1:TimerEvent):void
  314.          {
  315.             timer.removeEventListener(TimerEvent.TIMER,timerHandler);
  316.             timer.stop();
  317.             if(trustContent)
  318.             {
  319.                module.load(ApplicationDomain.currentDomain,SecurityDomain.currentDomain);
  320.             }
  321.             else
  322.             {
  323.                module.load();
  324.             }
  325.          };
  326.          timer.addEventListener(TimerEvent.TIMER,timerHandler,false,0,true);
  327.          timer.start();
  328.          return styleEventDispatcher;
  329.       }
  330.       
  331.       public function registerInheritingStyle(param1:String) : void
  332.       {
  333.          inheritingStyles[param1] = true;
  334.       }
  335.       
  336.       public function unloadStyleDeclarations(param1:String, param2:Boolean = true) : void
  337.       {
  338.          var _loc3_:StyleModuleInfo = null;
  339.          var _loc4_:IModuleInfo = null;
  340.          _loc3_ = styleModules[param1];
  341.          if(_loc3_)
  342.          {
  343.             _loc3_.styleModule.unload();
  344.             _loc4_ = _loc3_.module;
  345.             _loc4_.unload();
  346.             _loc4_.removeEventListener(ModuleEvent.READY,_loc3_.readyHandler);
  347.             _loc4_.removeEventListener(ModuleEvent.ERROR,_loc3_.errorHandler);
  348.             styleModules[param1] = null;
  349.          }
  350.          if(param2)
  351.          {
  352.             styleDeclarationsChanged();
  353.          }
  354.       }
  355.       
  356.       private function loadResources() : void
  357.       {
  358.          resourceUnableToLoad = packageResources.getString("unableToLoad");
  359.       }
  360.       
  361.       public function isInheritingStyle(param1:String) : Boolean
  362.       {
  363.          return inheritingStyles[param1] == true;
  364.       }
  365.       
  366.       public function get typeSelectorCache() : Object
  367.       {
  368.          return _typeSelectorCache;
  369.       }
  370.       
  371.       public function get stylesRoot() : Object
  372.       {
  373.          return _stylesRoot;
  374.       }
  375.       
  376.       public function getColorName(param1:Object) : uint
  377.       {
  378.          var _loc2_:Number = NaN;
  379.          var _loc3_:* = undefined;
  380.          if(param1 is String)
  381.          {
  382.             if(param1.charAt(0) == "#")
  383.             {
  384.                _loc2_ = Number("0x" + param1.slice(1));
  385.                return isNaN(_loc2_) ? StyleManager.NOT_A_COLOR : uint(_loc2_);
  386.             }
  387.             if(param1.charAt(1) == "x" && param1.charAt(0) == "0")
  388.             {
  389.                _loc2_ = Number(param1);
  390.                return isNaN(_loc2_) ? StyleManager.NOT_A_COLOR : uint(_loc2_);
  391.             }
  392.             _loc3_ = colorNames[param1.toLowerCase()];
  393.             if(_loc3_ === undefined)
  394.             {
  395.                return StyleManager.NOT_A_COLOR;
  396.             }
  397.             return uint(_loc3_);
  398.          }
  399.          return uint(param1);
  400.       }
  401.       
  402.       public function initProtoChainRoots() : void
  403.       {
  404.          if(!stylesRoot)
  405.          {
  406.             stylesRoot = selectors["global"].addStyleToProtoChain({},null);
  407.          }
  408.       }
  409.       
  410.       public function clearStyleDeclaration(param1:String, param2:Boolean) : void
  411.       {
  412.          var _loc3_:CSSStyleDeclaration = null;
  413.          _loc3_ = getStyleDeclaration(param1);
  414.          if(Boolean(_loc3_) && _loc3_.mx_internal::selectorRefCount > 0)
  415.          {
  416.             --_loc3_.mx_internal::selectorRefCount;
  417.          }
  418.          delete selectors[param1];
  419.          if(param2)
  420.          {
  421.             styleDeclarationsChanged();
  422.          }
  423.       }
  424.       
  425.       public function getStyleDeclaration(param1:String) : CSSStyleDeclaration
  426.       {
  427.          var _loc2_:int = 0;
  428.          if(param1.charAt(0) != ".")
  429.          {
  430.             _loc2_ = int(param1.lastIndexOf("."));
  431.             if(_loc2_ != -1)
  432.             {
  433.                param1 = param1.substr(_loc2_ + 1);
  434.             }
  435.          }
  436.          return selectors[param1];
  437.       }
  438.    }
  439. }
  440.  
  441. import flash.events.EventDispatcher;
  442. import mx.events.ModuleEvent;
  443. import mx.events.StyleEvent;
  444. import mx.modules.IModuleInfo;
  445.  
  446. class StyleEventDispatcher extends EventDispatcher
  447. {
  448.    public function StyleEventDispatcher(param1:IModuleInfo)
  449.    {
  450.       super();
  451.       param1.addEventListener(ModuleEvent.ERROR,moduleInfo_errorHandler,false,0,true);
  452.       param1.addEventListener(ModuleEvent.PROGRESS,moduleInfo_progressHandler,false,0,true);
  453.       param1.addEventListener(ModuleEvent.READY,moduleInfo_readyHandler,false,0,true);
  454.    }
  455.    
  456.    private function moduleInfo_readyHandler(param1:ModuleEvent) : void
  457.    {
  458.       var _loc2_:StyleEvent = null;
  459.       _loc2_ = new StyleEvent(StyleEvent.COMPLETE);
  460.       dispatchEvent(_loc2_);
  461.    }
  462.    
  463.    private function moduleInfo_errorHandler(param1:ModuleEvent) : void
  464.    {
  465.       var _loc2_:StyleEvent = null;
  466.       _loc2_ = new StyleEvent(StyleEvent.ERROR,param1.bubbles,param1.cancelable);
  467.       _loc2_.bytesLoaded = param1.bytesLoaded;
  468.       _loc2_.bytesTotal = param1.bytesTotal;
  469.       _loc2_.errorText = param1.errorText;
  470.       dispatchEvent(_loc2_);
  471.    }
  472.    
  473.    private function moduleInfo_progressHandler(param1:ModuleEvent) : void
  474.    {
  475.       var _loc2_:StyleEvent = null;
  476.       _loc2_ = new StyleEvent(StyleEvent.PROGRESS,param1.bubbles,param1.cancelable);
  477.       _loc2_.bytesLoaded = param1.bytesLoaded;
  478.       _loc2_.bytesTotal = param1.bytesTotal;
  479.       dispatchEvent(_loc2_);
  480.    }
  481. }
  482.  
  483. class StyleModuleInfo
  484. {
  485.    public var errorHandler:Function;
  486.    
  487.    public var module:IModuleInfo;
  488.    
  489.    public var styleModule:IStyleModule;
  490.    
  491.    public var readyHandler:Function;
  492.    
  493.    public function StyleModuleInfo(param1:IModuleInfo, param2:Function, param3:Function)
  494.    {
  495.       super();
  496.       this.module = param1;
  497.       this.readyHandler = param2;
  498.       this.errorHandler = param3;
  499.    }
  500. }
  501.